<?php

namespace soa\BasicInfo;


use soa\client;

class Shop extends client
{
    /**
     * 服务地址
     * @var string
     */
    public $service_url = 'http://core.kuaidihelp.com/';
    /**
     * 服务名称
     * @var string
     */
    protected $service_name = 'kuaidihelp_core';
    /*
     -------------------------------------------------------------------------------------------------------------------
     - 基础服务
     -------------------------------------------------------------------------------------------------------------------
     */
    /**
     * @param $index_shop_id
     * @return array|bool|null
     */
    public function getById($index_shop_id)
    {
        if(!empty($index_shop_id) && is_numeric($index_shop_id)) {
            return $this->get('v1/shop/info', ['index_shop_id' => $index_shop_id]);
        }
        return [];
    }


    /**
     * 根据 home_shop_id 获取网点信息   add by ljp
     * @param $home_shop_id
     * @return array
     */
    public function getByHomeId($home_shop_id)
    {
        if(!empty($home_shop_id) && is_numeric($home_shop_id)) {
            return $this->get('v1/shop/info', ['home_shop_id' => $home_shop_id]);
        }
        return [];
    }

    /**
     * 根据index_shop_id 获取当前网点下的子网点
     * @param $index_shop_id
     * @return array
     */
    public function getSubShopById($index_shop_id){

        if(!empty($index_shop_id) && is_numeric($index_shop_id)){
            return $this->get('v1/shop/info/getSubShop',['pid'=>$index_shop_id]);
        }
        return [];
    }

    /**
     * 根据网点名称获取网点
     * @param $name
     * @param $brand
     * @return array
     */
    public function getByName($name, $brand)
    {
        if(empty($name) || empty($brand)) {
            return [];
        }
        return $this->post('v1/shop/info',
            [
                'index_shop_name' => $name,
                'brand' => $brand
            ]
        ) ?: [];
    }


    /**
     * @param array $dict
     * @return int
     */
    public function add(array $dict)
    {
        if(!empty($dict)) {
            return $this->get('v1/shop/add', $dict) ?: 0;
        }
        return false;
    }

    /**
     * @param $index_shop_id
     * @return bool
     */
    public function del($index_shop_id)
    {
        if(!empty($index_shop_id) && is_numeric($index_shop_id)) {
            return $this->get('v1/shop/update', ['index_shop_id' => $index_shop_id, 'is_deleted' => 1]) ? true : false;
        }
        return false;
    }

    /**
     * @param $index_shop_id
     * @param array $dict
     * @return bool
     */
    public function update($index_shop_id, array $dict)
    {
        if(!empty($index_shop_id) && is_numeric($index_shop_id) && !empty($dict)) {
            $res = $this->get('v1/shop/update', array_merge($dict, ['index_shop_id' => $index_shop_id]));

            return $res ? true : false;
        }
        return false;
    }

    /**
     * 根据网点工号获取网点的home_shop_id
     * @param $org_code
     * @param string $brand
     * @return string
     */
    public function orgCode2shopID($org_code,$brand = "sto"){
        if(empty($org_code))
            return "";

        $indexShopId = $this->orgCode2indexID($org_code,$brand);

        $shopInfo = $this->getById($indexShopId);

        return $shopInfo['home_shop_id'];
    }

    /**
     * 根据网店工号获取网点的index_shop_id
     * @param $org_code
     * @param string $brand
     * @return string
     */
    public function orgCode2indexID($org_code,$brand = "sto"){
        if(empty($org_code))
            return "";

        $branchShopInfo = $this->get('v1/shop/info/branchInfo', array("orgCode" => $org_code,"brand" => $brand));

        if($branchShopInfo["MainID"])
            return $branchShopInfo["MainID"];

        return "";
    }

    /**
     * 根据网点的index_shop_id获取网点工号
     * @param $index_shop_id
     * @param string $brand
     * @return string
     */
    public function indexID2orgCode($index_shop_id,$brand = "sto"){
        if(empty($index_shop_id))
            return "";

        $branchShopInfo = $this->get('v1/shop/info/branchInfo', array("indexShopId" => $index_shop_id,"brand" => $brand));

        if($branchShopInfo["BranchCode"])
            return $branchShopInfo['BranchCode'];

        return "";
    }

    /*
     * 检查是否是独立网点
     * @param $index_shop_id
     * @param $brand
     * return boolean
     * */
    public function isIndependent($index_shop_id , $brand = 'sto')
    {
        $shop = $this->getById($index_shop_id);

        switch ($brand)
        {
//            case 'zt':
//                return false;
            default:
                $res =  $shop['pid'] == '74594'  ? true : false;
                break;
        }

        return $res;

    }

    /*
     * 根据网点编号获取网点信息
     * @param $branchCode
     * @param $brand
     * $return string
     * */
    public function getByBranchCode($branchCode = ' ' , $brand = 'sto')
    {
        return $this->getById($this->orgCode2indexID($branchCode,$brand));
    }

    /*
     * 根据网点编号获取branchInfo
     * @param $branchCode
     * @param $brand
     * $return array
     * */
    public function getBranchInfoByBranchCode($branchCode = '' , $brand = 'sto')
    {
        return $this->get('v1/shop/info/branchInfo', array("orgCode" => $branchCode,"brand" => $brand));
    }

    /*
     * 根据网点ID获取branchIno
     * @param $indexShopID
     * @param $brand
     * @return array
     * */
    public function getBranchInfoByIndexId($index_shop_id = 0 , $brand = 'sto')
    {
        return $this->get('v1/shop/info/branchInfo', array("indexShopId" => $index_shop_id));
    }

    /**
     * 根据home_shop_id 更新     add by ljp
     * @param $home_shop_id
     * @param array $dict
     * @return bool
     */
    public function updateWithHomeId($home_shop_id, array $dict)
    {
        if(!empty($home_shop_id) && is_numeric($home_shop_id) && !empty($dict)) {
            return $this->get('v1/shop/update', array_merge($dict, ['home_shop_id' => $home_shop_id])) ? true : false;
        }
        return false;
    }

    /**
     * 新版网点检索匹配 暂时不支持GEO匹配
     * @param array $dict
     * @return bool
     */
    public function search(
        // 参数示例
        array $dict = [
            'geo' => [],
            'page' => 1,
            'page_num' => 30,
            'address' => '',
            'area_id' => 0,
            'accept_order' => 0,
            'verified' => 0
        ]
    )
    {
        return $this->post('v1/shop/search', ['condition' => json_encode($dict)]);
    }
}